package chargemanager

import (
	"bytes"
	"encoding/binary"
)

func startChargeControlSignal(chargeId uint8) ChargerControlWord {
	return ChargerControlWord{ID: chargeId, Control: 01, Reserved1: 0, Reserve2: 0}
}

func finishChargeControlSignal(chargeId uint8) ChargerControlWord {
	return ChargerControlWord{ID: chargeId, Control: 02, Reserved1: 0, Reserve2: 0}
}

type ChargerControlWord struct {
	ID        uint8
	Control   byte
	Reserved1 uint8
	Reserve2  uint8
}

func (word ChargerControlWord) Bytes() []byte {
	data := &bytes.Buffer{}
	binary.Write(data, binary.BigEndian, frameHeader)
	binary.Write(data, binary.BigEndian, word.ID)
	binary.Write(data, binary.BigEndian, word.Control)
	binary.Write(data, binary.BigEndian, word.Reserved1)
	binary.Write(data, binary.BigEndian, word.Reserve2)
	binary.Write(data, binary.BigEndian, getCrc16(word.ID, word.Control, word.Reserved1, word.Reserve2))
	binary.Write(data, binary.BigEndian, frameTail)
	return data.Bytes()
}
